<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Main extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->model ( 'project_model' );
		$this->load->model ( 'user_model' );
		$this->load->model('category_model');
		$whereArr = array("status"=>1);
		$this->category_list = $this->category_model->resultAll($whereArr);
	}
	/*
	 *加载首页 
	 */
	public function index($cid=0,$page=1){
		if($cid==0){
			$whereArr = array("status"=>1);
		}else{
			$whereArr = array("cid"=>$cid,"status"=>1);
		}
		$project = $this->project_model->resultAll($whereArr,"starttime@DESC");
		$newProject = array();
		$i = 0;
		foreach($project as $key => $value){
			$flag = $this->count_day($value['starttime'],$value['addtime']);
			if($flag){
				$newProject[$i] = $project[$key];
				$i++;
			}
		}
		$count = count($newProject);
		$params = array(
				"url_template" => site_url ( "Main/index/{$cid}/{page}" ),
				"page_count" => $count,
				"page_now" => $page,
				"page_size" => 4,
				"class"=>array("ul"=>"","disabled"=>"am-disabled","active"=>"am-active")
		);
		$params["total_pages"] = ceil ( $params['page_count'] / $params['page_size'] );
		$this->load->library ( 'paging', $params );
		$this->load->library ('ArrayChange');
		$newProject_list = $this->arraychange->ArrayPage($newProject,$page,4);
		foreach ($newProject_list as $key => $value){
			$newProject_list[$key]['uid']=$this->changeUserName($value['uid']);
			$newProject_list[$key]['logo'] = ltrim($value['logo'],".");
		}
		$data['newProject_list'] = $newProject_list;
		$data['category_list'] = $this->category_list;
		$this->load->view("main",$data);
	}
	/*
	 *加载创建校园众创项目视图 
	 */
	public function add_project(){
		$this->tologin->isLogin ( site_url ( 'login' ) );
		$data['category_list'] = $this->category_list;
		$this->load->view("project_create",$data);
	}
	/*
	 *创建校园众创项目 
	 */
	public function do_add_project(){
		
		if(empty($_FILES['project_logo']['name'])){
			$data['message'] = "你还没有上传图片";
			$this->load->view("assists/error",$data);
		}else{
			$params=array(
				"save_path"=>"./data/uploads/project_images/",
				"resource"=>$_FILES['project_logo']
			);
			$this->load->library("ImageUpload.php",$params);
			$logo = $this->imageupload->upload();
			
			$data["cid"] = $this->input->post("cid");
			$data["uid"] = $this->session->userdata('uid');
			$data['title'] = $this->input->post("title");
			$data["logo"] = $logo;
			$data["starttime"] = time();
			$data["addtime"] = $this->input->post("addtime");
			$data["personNum"] = $this->input->post("personNum");
			$result = $this->project_model->insert($data);
			if($result){
				$data['url'] = site_url("Main");
				$data['message'] = "项目创建成功!!";
				$this->load->view("assists/success",$data);
			}else{
				$data['message'] = "项目创建失败!!";
				$this->load->view("assists/error",$data);
			}
		}
	}
	/*
	 *根据用户id查找用户名
	 */
	public function changeUserName($uid){
		$whereArr = array("uid"=>$uid);
		$user_row = $this->user_model->row($whereArr);
		return $user_row['username'];
	}
	/*
	 *计算项目剩余天数
	 */
	public function count_day($starttime,$addtime){
		$nowtime = time();
		$time_difference_day = round(($nowtime - $starttime)/3600/24);
		if($time_difference_day<$addtime){
			return ($addtime-$time_difference_day);
		}else{
			return false;
		}
	}
}